package com.sais.manage.boot.platform.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sais.common.utils.UUIDUtils;
import com.sais.manage.boot.platform.dto.RoleDTO;
import com.sais.manage.boot.platform.dto.RoleSaveDTO;
import com.sais.manage.boot.platform.dto.RoleSearchDTO;
import com.sais.manage.boot.platform.entity.Role;
import com.sais.manage.boot.platform.mapper.RoleMapper;
import com.sais.manage.boot.platform.service.IMenuService;
import com.sais.manage.boot.platform.service.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 宿舍管理系统-角色 service 实现类
 *
 * @author zhang
 * @date 2019/11/29
 **/
@Service
@Slf4j
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private IMenuService menuService;


    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveRole(RoleSaveDTO roleSaveDTO) {
        String ukId = roleSaveDTO.getUkId();
        if (StringUtils.isEmpty(ukId)) {
            this.createRole(roleSaveDTO);
        } else {
            this.updateRole(roleSaveDTO);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean createRole(RoleSaveDTO roleSaveDTO) {
        Role role = roleSaveDTO.convert();
        role.setUkId(UUIDUtils.getLower());
        roleMapper.createRole(role);

        List<String> menuIds = roleSaveDTO.getMenuIds();
        if (!CollectionUtils.isEmpty(menuIds)) {
            this.bindRoleMenu(role.getUkId(), menuIds);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateRole(RoleSaveDTO roleSaveDTO) {
        Role role = roleSaveDTO.convert();
        role.setGmtModified(LocalDateTime.now());
        roleMapper.updateById(role);

        //更新角色对应菜单 先删除角色下绑定所有菜单 然后重新添加
        List<String> menus = roleSaveDTO.getMenuIds();
        if (!CollectionUtils.isEmpty(menus)) {
            this.unBindRoleMenu(role.getUkId());
            this.bindRoleMenu(role.getUkId(), menus);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteRole(String roleId) {
        this.unBindRoleMenu(roleId);
        roleMapper.deleteRole(roleId);
        return true;
    }

    @Override
    public RoleDTO selectRole(String roleId) {
        RoleDTO role = roleMapper.selectRole(roleId);
        return role;
    }

    @Override
    public Page<RoleDTO> listRole(Integer current, Integer size, RoleSearchDTO roleSearchDTO) {
        Page<Role> page = new Page<>(current, size);
        Page<RoleDTO> rolePage = roleMapper.listRole(page, roleSearchDTO);
        return rolePage;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean bindRoleMenu(String roleId, List<String> menuIds) {
        roleMapper.bindRoleMenu(roleId, menuIds);
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean unBindRoleMenu(String roleId) {
        roleMapper.unBindRoleMenu(roleId);
        return true;
    }
}
